from app import create_app, db
from app.models import User, WorkflowTemplate, FormDefinition

app = create_app()

with app.app_context():
    # 创建所有表
    db.create_all()
    
    # 先清空用户表
    User.query.delete()
    db.session.commit()
    
    # 创建测试用户
    admin = User(username='admin', role='admin')
    admin.set_password('admin123')
    db.session.add(admin)
    
    manager = User(username='manager', role='manager', department='IT')
    manager.set_password('manager123')
    db.session.add(manager)
    
    hr = User(username='hr', role='hr')
    hr.set_password('hr123')
    db.session.add(hr)
    
    employee = User(username='employee', role='employee', department='IT')
    employee.set_password('employee123')
    db.session.add(employee)
    
    db.session.commit()
    
    # 清空模板和表单表
    WorkflowTemplate.query.delete()
    FormDefinition.query.delete()
    db.session.commit()
    
    # 创建示例流程模板
    leave_process_xml = '''<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn">
  <bpmn:process id="Process_1" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1" name="开始">
      <bpmn:outgoing>Flow_1</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_1" sourceRef="StartEvent_1" targetRef="Task_1" />
    <bpmn:userTask id="Task_1" name="部门主管审批">
      <bpmn:incoming>Flow_1</bpmn:incoming>
      <bpmn:outgoing>Flow_2</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:sequenceFlow id="Flow_2" sourceRef="Task_1" targetRef="Task_2" />
    <bpmn:userTask id="Task_2" name="人事审批">
      <bpmn:incoming>Flow_2</bpmn:incoming>
      <bpmn:outgoing>Flow_3</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:sequenceFlow id="Flow_3" sourceRef="Task_2" targetRef="EndEvent_1" />
    <bpmn:endEvent id="EndEvent_1" name="结束">
      <bpmn:incoming>Flow_3</bpmn:incoming>
    </bpmn:endEvent>
  </bpmn:process>
</bpmn:definitions>'''

    template = WorkflowTemplate(
        name='请假申请流程',
        description='员工请假流程，需要部门主管和人事审批',
        bpmn_xml=leave_process_xml,
        status='published',
        created_by=admin.id
    )
    db.session.add(template)
    
    # 创建示例表单定义
    leave_form = FormDefinition(
        key='leave_request_form',
        name='请假申请表单',
        schema={
            "type": "object",
            "required": ["leaveType", "startDate", "endDate", "reason"],
            "properties": {
                "leaveType": {
                    "type": "string",
                    "title": "请假类型",
                    "enum": ["年假", "病假", "事假", "调休"],
                    "default": "年假"
                },
                "startDate": {
                    "type": "string",
                    "format": "date",
                    "title": "开始日期"
                },
                "endDate": {
                    "type": "string",
                    "format": "date",
                    "title": "结束日期"
                },
                "reason": {
                    "type": "string",
                    "title": "请假原因",
                    "maxLength": 200
                }
            }
        }
    )
    db.session.add(leave_form)
    
    approval_form = FormDefinition(
        key='leave_approval_form',
        name='请假审批表单',
        schema={
            "type": "object",
            "required": ["approved", "comment"],
            "properties": {
                "approved": {
                    "type": "boolean",
                    "title": "是否同意",
                    "default": True
                },
                "comment": {
                    "type": "string",
                    "title": "审批意见",
                    "maxLength": 200
                }
            }
        }
    )
    db.session.add(approval_form)
    
    db.session.commit()
    
    # 验证用户是否创建成功
    print(f'用户总数: {User.query.count()}')
    for user in User.query.all():
        print(f'用户名: {user.username}, 角色: {user.role}')
        
    print(f'\n流程模板总数: {WorkflowTemplate.query.count()}')
    for template in WorkflowTemplate.query.all():
        print(f'模板名称: {template.name}, 状态: {template.status}')
        
    print(f'\n表单定义总数: {FormDefinition.query.count()}')
    for form in FormDefinition.query.all():
        print(f'表单名称: {form.name}, 键名: {form.key}') 